package model;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import exciptions.DublicatedUserExcitpion;
import exciptions.InvalidPasswordtException;
import exciptions.UserNotExistExcitpion;

public class UsersManager {

	public static ArrayList<User> users;

	public UsersManager() {
		users = new ArrayList<User>();
		getXML();
	}

	public void getXML() {
		// TODO Auto-generated method stub
		boolean eof = false;
		String in;

		try {
			BufferedReader file = new BufferedReader(new FileReader("user.xml"));
			while (!eof) {
				in = file.readLine();

				if (in == null) {
					eof = true;
					break;
				}
				// System.out.println(in);
				insertUser(in);
			}
			file.close();
		//	printAll();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidPasswordtException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void insertUser(String line) throws InvalidPasswordtException {

		if (line.indexOf("<loginName>") != -1) {
			String logName = line.substring(line.indexOf(">") + 1,
					line.indexOf("</loginName>"));
			users.add(new User(logName));
		} else if (line.indexOf("<password>") != -1) {
			String pass = line.substring(line.indexOf(">") + 1,
					line.indexOf("</password>"));
			users.get(users.size() - 1).setPassword(pass);
		} else if (line.indexOf("<name>") != -1) {
			String name = line.substring(line.indexOf(">") + 1,
					line.indexOf("</name>"));
			users.get(users.size() - 1).setName(name);
		} else if (line.indexOf("<email>") != -1) {
			String email = line.substring(line.indexOf(">") + 1,
					line.indexOf("</email>"));
			users.get(users.size() - 1).setEmail(email);
		}
	}

	public void printAll() {
		for (User i : users) {
			System.out.println(i.toString());
		}
	}

	public void writeXML() {

		try {
			DocumentBuilderFactory docFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			

			Document doc = docBuilder.newDocument();
			//doc.setPrefix(prefix)
			Element rootElement = doc.createElement("Users");
			doc.appendChild(rootElement);

			for (User i : users) {
				Element user = doc.createElement("user");
				rootElement.appendChild(user);

				Element logIn = doc.createElement("loginName");
				logIn.appendChild(doc.createTextNode(i.getLoginName()));
				user.appendChild(logIn);

				Element pass = doc.createElement("password");
				pass.appendChild(doc.createTextNode(i.getPassword()));
				user.appendChild(pass);

				Element name = doc.createElement("name");
				name.appendChild(doc.createTextNode(i.getName()));
				user.appendChild(name);

				Element email = doc.createElement("email");
				email.appendChild(doc.createTextNode(i.getEmail()));
				user.appendChild(email);
			}


			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new File("user.xml"));
			transformer.transform(source, result);
			
			
			

		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public User getUser(String logName, String pass) throws UserNotExistExcitpion{
		for (User i : users) {

			if (i.getLoginName().equals(logName) && i.getPassword().equals(pass)){
				System.out.println(i.getLoginName()+" "+i.getPassword());
				return i;
			}
		}
		throw new UserNotExistExcitpion("No such a user with this login name and password");
	}
	
	
	public void addUser(User user) throws DublicatedUserExcitpion{
		for (User i : users) {
			if (i.getLoginName().equals(user.getLoginName())){
				throw new DublicatedUserExcitpion("Log in name "+user.getLoginName()+" is already exist in the database");
			}
		}
		users.add(user);
	}

}
